<?php

define('LR_MERCHANT_SECURIRY_WORD', 'lodestore123');
define('LR_MERCHANT_STORE', 'LoDeStore');
define('LR_ACC_ID', 'U2530753');

$link = mysql_connect('db.hostvn.net', 'vocitvn_lode', 'lode');
mysql_select_db('vocitvn_lode', $link);

$str = "Deposit data".print_r($_REQUEST, TRUE);
$query = "INSERT INTO ld_log(category, level, message, log_date) VALUES ('status.php','info','$str',NOW())";
mysql_query($query, $link);

$str = $_REQUEST["lr_paidto"].":".$_REQUEST["lr_paidby"].":".stripslashes($_REQUEST["lr_store"]).":".
        $_REQUEST["lr_amnt"].":".$_REQUEST["lr_transfer"].":".$_REQUEST["lr_currency"].":".LR_MERCHANT_SECURIRY_WORD;

//Calculating hash
$hash = strtoupper(bin2hex(mhash(MHASH_SHA256, $str)));

$query = "INSERT INTO ld_log(category, level, message, log_date) VALUES ('status.php','info','$str',NOW())";
mysql_query($query, $link);

$query = "INSERT INTO ld_log(category, level, message, log_date) VALUES ('status.php','info','hash tính toán: $hash',NOW())";
mysql_query($query, $link);

if (isset($_REQUEST["lr_paidto"]) && $_REQUEST["lr_paidto"] == strtoupper(LR_ACC_ID) &&
        isset($_REQUEST["lr_store"]) && stripslashes($_REQUEST["lr_store"]) == LR_MERCHANT_STORE &&
        isset($_REQUEST["lr_encrypted"]) && $_REQUEST["lr_encrypted"] == $hash)
{
    // Payment was verified and is successful

    $lr_amnt = isset($_REQUEST['lr_amnt']) ? $_REQUEST['lr_amnt'] : 0;
    $user_id = isset($_REQUEST['uuid']) ? $_REQUEST['uuid'] : '';
    if ($user_id != '')
    {
        $query2 = "UPDATE ld_user SET lr_money = lr_money + $lr_amnt WHERE user_id='$user_id'";
        if (mysql_query($query2, $link))
        {
            $query = "INSERT INTO ld_log(category, level, message, log_date) VALUES ('status.php','info','Nạp tiền thành công cho user có user_id = $user_id. Thêm $lr_amnt LRUSD',NOW())";
            mysql_query($query, $link);
            
            // Lưu log nạp tiền
            $query = "INSERT INTO ld_deposit_history(user_id, deposit_date, lr_amount, lr_currency) VALUES ('{$user_id}', NOW(), '{$lr_amnt}', '{$_REQUEST['lr_currency']}')";
            mysql_query($query, $link);
        }
    }
}
else
{
    $query = "INSERT INTO ld_log(category, level, message, log_date) VALUES ('status.php','debug','Nap tien bat thuong',NOW())";
    mysql_query($query, $link);
}
?>